<?php

class Comment extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'tbl_comment':
	 * @var integer $id
	 * @var string $content
	 * @var integer $status
	 * @var integer $create_time
	 * @var string $author
	 * @var string $email
	 * @var string $url
	 * @var integer $post_id
	 */
	const STATUS_PENDING=1;
	const STATUS_APPROVED=2;

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{comment}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('content, author, email, status', 'required'),
			array('author, email, url', 'length', 'max'=>128),
			array('email','email'),
			array('url','url'),
		);
	}

	public function accessRules()
	{
		return array(
			array('allow', // allow authenticated users to access all actions
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'post' => array(Post::BELONGS_TO, 'Post', 'post_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'content' => 'Comentário',
			'status' => 'Státus',
			'create_time' => 'Create Time',
			'author' => 'Nome',
			'email' => 'Email',
			'url' => 'Website',
			'post_id' => 'Post',
		);
	}

	/**
	 * Approves a comment.
	 */
	public function approve()
	{
		$this->status=Comment::STATUS_APPROVED;
		$this->update(array('status'));
	}

	/**
	 * @param Post the post that this comment belongs to. If null, the method
	 * will query for the post.
	 * @return string the permalink URL for this comment
	 */
	public function getUrl($post=null)
	{
		if($post===null)
			$post=$this->post;
		return $post->url.'#c'.$this->id;
	}

	/**
	 * @return string the hyperlink display for the current comment's author
	 */
	public function getAuthorLink()
	{
		if(!empty($this->url))
			return CHtml::link(CHtml::encode($this->author),$this->url);
		else
			return CHtml::encode($this->author);
	}

	/**
	 * @return integer the number of comments that are pending approval
	 */
	public function getPendingCommentCount()
	{
		return $this->count('status='.self::STATUS_PENDING);
	}

	/**
	 * @param integer the maximum number of comments that should be returned
	 * @return array the most recently added comments
	 */
	public function findRecentComments($limit=5)
	{

            $sql = 'select *  from '.$this->tableName().' order by create_time desc limit 5';
            $command = Yii::app()->db->createCommand($sql)->queryAll();

           return $command;
            
	}

	public function loadCommentMenu()
	{
          $ar = array();
          $comments = array();
          $comments = $this->findRecentComments();
          
          $ar[] = array('label'=>'Todos ...', 'url'=>array('post/index'), 'linkOptions' => array( 'class' => 'menulink' ));

          if(count($comments)>0)
           foreach($comments as $comment){
               $ar[] = array('label'=>substr($comment['content'], 0, 16).'..', 'url'=>array('post/view&id='.$comment['post_id']), 'linkOptions' => array( 'class' => 'menulink' ));
           }


	  return $ar;
	}
	/**
	 * This is invoked before the record is saved.
	 * @return boolean whether the record should be saved.
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
				$this->create_time=time();
			return true;
		}
		else
			return false;
	}

	public function mySearch()
	{
		$criteria=new CDbCriteria;

                $criteria->compare('id',$this->id,true);
		$criteria->compare('content',$this->content,true);
                $criteria->compare('author',Yii::app()->user->name,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider('Comment', array(
			'criteria'=>$criteria,
			'sort'=>array(
				'defaultOrder'=>'status, create_time DESC',
			),
		));
	}

	public function search()
	{
		$criteria=new CDbCriteria;

                $criteria->compare('id',$this->id,true);
		$criteria->compare('content',$this->content,true);
                $criteria->compare('author',$this->author,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider('Comment', array(
			'criteria'=>$criteria,
			'sort'=>array(
				'defaultOrder'=>'status, create_time DESC',
			),
		));
	}
}